var spec_index;
var first_spec;
var last_spec;
var selected_spec=new Array();
var default_spec=new Array();
var my_site=SITE_URL;
var spec_1 = new Array();
/* spec对象 */
// lichen 2019/4/18 start
function spec(id, spec1,spec_1value, spec2, spec3, spec4, price, tax_price, pro_price, stock, is_pro,discount)
// function spec(id, spec1, spec2, spec3, spec4, price, pro_price, stock, is_pro,discount)
// lichen 2019/4/18 end
{
    this.id = id;
    this.spec1 = spec1;
    this.spec_1value = spec_1value;
    this.spec2 = spec2;
    this.spec3 = spec3;
    this.spec4 = spec4;
    this.price = price;
    // lichen 2019/4/18 start
    this.tax_price = tax_price;
    // lichen 2019/4/18 end
    this.pro_price = pro_price;
    this.stock = stock;
    this.is_pro = is_pro;
    this.discount=discount;
}

/* goodsspec对象 */
function goodsspec(specs, specQty, defSpec)
{
    this.specs = specs;
    this.specQty = specQty;
    this.defSpec = defSpec;
    this.spec1 = '';
    this.spec2 = '';
    this.spec3 = '';
    this.spec4 = '';
    // lichen 2019/4/24 start
    // if (this.specQty >= 1)
    // {
    // lichen 2019/4/24 end
        for (var i = 0; i < this.specs.length; i++)
        {
            if (this.specs[i].id == this.defSpec)
            {
                this.spec1 = this.specs[i].spec1;
                this.spec2 = this.specs[i].spec2;
                this.spec3 = this.specs[i].spec3;
                this.spec4 = this.specs[i].spec4;
                if(this.spec1){
                   default_spec[1] =this.spec1;
                }
                if(this.spec2){
                   default_spec[2] =this.spec2;
                }
                if(this.spec3){
                   default_spec[3] =this.spec3;
                }
                if(this.spec4){
                   default_spec[4] =this.spec4;
                }
                break;
            }
        }
    // lichen 2019/4/24 start
    // }
    // lichen 2019/4/24 end
    // 取得某字段的不重复值，如果有spec1，以此为条件
    this.getDistinctValues = function (field,selected)
    {
        var values = new Array();
        spec_1 = new Array();
        for (var i = 0; i < this.specs.length; i++)
        {

            var value = this.specs[i][field];
            var continue_flag=false;
            if (selected != ''){
                    for(var k in selected){
                        switch(k){
                            // case '1':
                            //
                            //     if(selected[k] != this.specs[i].spec1){
                            //         continue_flag=true;
                            //     }
                            //     break;
                            case '_1value':

                                if(selected[k] != this.specs[i].spec_1value){
                                    continue_flag=true;
                                }
                                break;
                            case '2':
                                if(selected[k] != this.specs[i].spec2){
                                    continue_flag=true;
                                }
                                break;
                            case '3':
                                if(selected[k] != this.specs[i].spec3){
                                    continue_flag=true;
                                }
                                break;
                            case '4':
                                if(selected[k] != this.specs[i].spec4){
                                    continue_flag=true;
                                }
                                break;
                            case '1':

                                if(selected[k] != this.specs[i].spec1){
                                    continue_flag=true;
                                }
                                break;
                        }

                    }
                if(continue_flag){
                    continue;
                }
            }

            if (value && $.inArray(value, values) < 0)
            {
                values.push(value);
                if (field == 'spec_1value'){
                    spec_1.push(specs[i]['spec1'])
                }
            }
        }
        return (values);
    }

    // 取得选中的spec
    this.getSpec = function ()
    {
        for (var i = 0; i < this.specs.length; i++)
        {
            if ((this.specs[i].spec1 == this.spec1) && (this.specs[i].spec2 == this.spec2) && (this.specs[i].spec3 == this.spec3) && (this.specs[i].spec4 == this.spec4))
                return this.specs[i];
        }
        return '';
    }

    // 初始化
    this.init = function ()
    {
        spec_index=0;
        spec_index=3;
        // if(this.spec1 != ''){
        //     first_spec=this.spec1;
        //     spec_index=1;
        // }else if(this.spec2 != ''){
        //     first_spec=this.spec2;
        //     spec_index=2;
        // }else if(this.spec3 != ''){
        //     first_spec=this.spec3;
        //     spec_index=3;
        // }else if(this.spec4 != ''){
        //     first_spec=this.spec4;
        //     spec_index=4;
        // }
        if(this.spec4 != ''){
            last_spec=4;
        }else if(this.spec3 != ''){
            last_spec=3;
        }else if(this.spec2 != ''){
            last_spec=2;
        }else if(this.spec1 != ''){
            last_spec=1;
        }
		if(!spec_index){
			return;
		}
        // var spec1 = this.getDistinctValues('spec1','');
        var spec1Values = this.getDistinctValues('spec_1value','');
        for (var i = 0; i < spec1Values.length; i++)
            {

                    if (spec1Values[i] == this.spec_1values)
                    {
                        $(".handle ul:eq(0)").append("<li style='height:40px;padding:0;' class='solid' onclick='selectSpec(1, this)'><a href='javascript:;'><span><img src='" +my_site.replace("/mobile", "")+"/" +  spec_1[i]+ "' /></span></a></li>");
                    }
                    else
                    {
                        $(".handle ul:eq(0)").append("<li style='height:40px;padding:0;' class='dotted' onclick='selectSpec(1, this)'><a href='javascript:;'><span><img src='" +my_site.replace("/mobile", "")+"/" +  spec_1[i]+ "' /></span></a></li>");
                    }
            }

            var now_select1=new Array();
            for(var k=0;k<1;k++){
                if(typeof(default_spec[k+1]) != "undefined"){
                    now_select1[k+1]=default_spec[k+1];
                }
            }
            var spec2Values = this.getDistinctValues('spec2', '');
            console.log(spec_index)
            for (var i = 0; i < spec2Values.length; i++)
            {
                if (spec2Values[i] == this.spec2)
                {
                    $("#handle_2").append("<li class='solid' onclick='selectSpec(2, this)'><a href='javascript:;'><span>" + spec2Values[i] + "</span></a></li>");
                }
                else
                {
                    $("#handle_2").append("<li class='dotted' onclick='selectSpec(2, this)'><a href='javascript:;'><span>" + spec2Values[i] + "</span></a></li>");
                }
            }
            var now_select2=new Array();
            for(var k=0;k<2;k++){
                if(typeof(default_spec[k+1]) != "undefined"){
                    now_select2[k+1]=default_spec[k+1];
                }
            }
            // var spec3Values = this.getDistinctValues('spec3', (spec_index==3)?'':now_select2);
        var spec3Values = this.getDistinctValues('spec3', '');
            for (var i = 0; i < spec3Values.length; i++)
            {
                if (spec3Values[i] == this.spec3)
                {
                    $("#handle_3").append("<li class='solid' onclick='selectSpec(3, this)'><a href='javascript:;'><span>" + spec3Values[i] + "</span></a></li>");
                }
                else
                {
                    $("#handle_3").append("<li class='dotted' onclick='selectSpec(3, this)'><a href='javascript:;'><span>" + spec3Values[i] + "</span></a></li>");
                }
            }
            var now_select3=new Array();
            for(var k=0;k<3;k++){
                if(typeof(default_spec[k+1]) != "undefined"){
                    now_select3[k+1]=default_spec[k+1];
                }
            }
            var spec4Values = this.getDistinctValues('spec4', (spec_index==4)?'':now_select3);
            for (var i = 0; i < spec4Values.length; i++)
            {
                if (spec4Values[i] == this.spec4)
                {
                    $("#handle_4").append("<li class='solid' onclick='selectSpec(4, this)'><a href='javascript:;'><span>" + spec4Values[i] + "</span></a></li>");
                }
                else
                {
                    $("#handle_4").append("<li class='dotted' onclick='selectSpec(4, this)'><a href='javascript:;'><span>" + spec4Values[i] + "</span></a></li>");
                }
            }
        var spec = this.getSpec();
        $("[ectype='current_spec']").html(((spec.spec_1value != '')?spec.spec_1value + ' ':'') + ((spec.spec2 != '')?spec.spec2 + ' ':'') + ((spec.spec3 != '')?spec.spec3 + ' ':'') + ((spec.spec4 != '')?spec.spec4 + ' ':'') );
    }
}

/* 选中某规格 num=1,2  */
function selectSpec(num, liObj)
{
    if(num==1){
			var temp_spec1 = $(liObj).find('a span img').attr('src');
        $('.big_pic img').attr('src', temp_spec1);
        $('.big_pic img').attr('jqimg', temp_spec1.replace('goodsspec/','goodsspec/'));
                        goodsspec['spec' + num] = temp_spec1.replace(my_site.replace("/mobile", "")+'/', "");
                        goodsspec['spec' + num] = goodsspec['spec' + num].replace('goodsspec/','goodsspec/');
    }else{
        goodsspec['spec' + num] = $(liObj).find('a span').html();
    }

    selected_spec[num]=goodsspec['spec' + num];
    var now_select=new Array();
    for(var k=0;k<num;k++){
        if(typeof(selected_spec[k+1]) != "undefined"){
            now_select[k+1]=selected_spec[k+1];
        }
    }
    $(liObj).attr("class", "solid");
    $(liObj).siblings(".solid").attr("class", "dotted");

    // 当有2种规格并且选中了第一个规格时，刷新第二个规格
    if ((num < last_spec))
    {
        console.log(now_select)
        $(".aggregate").html("");
        for(var k=num;k<4;k++){
            eval('goodsspec.spec'+(k+1)+' = "";');
            $("#handle_"+(k+1)+" li[class='handle_title']").siblings().remove();

        }
        if(num < 2){
            var spec2Values = goodsspec.getDistinctValues('spec2','');
            for (var i = 0; i < spec2Values.length; i++)
            {
                    $("#handle_2").append("<li class='dotted' onclick='selectSpec(2, this)'><a href='javascript:;'><span>" + spec2Values[i] + "</span></a></li>");
            }
        }
        if(num < 3){
            var spec3Values = goodsspec.getDistinctValues('spec3','');
            for (var i = 0; i < spec3Values.length; i++)
            {
                    $("#handle_3").append("<li class='dotted' onclick='selectSpec(3, this)'><a href='javascript:;'><span>" + spec3Values[i] + "</span></a></li>");
            }
        }
        if(num < 4){
            var spec4Values = goodsspec.getDistinctValues('spec4','');
            for (var i = 0; i < spec4Values.length; i++)
            {
                    $("#handle_4").append("<li class='dotted' onclick='selectSpec(4, this)'><a href='javascript:;'><span>" + spec4Values[i] + "</span></a></li>");
            }
        }
    }
    else
    {
        var spec = goodsspec.getSpec();
        if (spec != '')
        {
            $("[ectype='current_spec']").html(((spec.spec_1value != '')?spec.spec_1value + ' ':'') + ((spec.spec2 != '')?spec.spec2 + ' ':'') + ((spec.spec3 != '')?spec.spec3 + ' ':'') + ((spec.spec4 != '')?spec.spec4 + ' ':''));
            $("[ectype='goods_price']").html(price_format(spec.price));
			$("[ectype='market_price']").html(price_format(market_ratio*spec.price));
			// lichen 2019/4/18 start
            $("[ectype='tax_price']").html(price_format(spec.tax_price));
            // lichen 2019/4/18 end
            if (spec.discount <= 1 && spec.discount > 0) {
                $("[ectype='member_price']").html(price_format(spec.price * spec.discount));
            }
            if(spec.is_pro){
                $("[ectype='goods_pro_price']").html(price_format(spec.pro_price));
				$("[ectype='is_pro']").show();
            }else{
				$("[ectype='is_pro']").hide();
			}
            $("[ectype='goods_stock']").html(spec.stock);
        }
    }
}
function slideUp_fn()
{
    $('.ware_cen').slideUp('slow');
}
$(function () {
    goodsspec.init();

    //放大镜效果/
    if ($(".jqzoom img").attr('jqimg'))
    {
        $(".jqzoom").jqueryzoom({xzoom: 450, yzoom: 450});
    }

    // 图片替换效果
    $('.ware_box li').mouseover(function () {
        $('.ware_box li').removeClass();
        $(this).addClass('ware_pic_hover');
        $('.big_pic img').attr('src', $(this).children('img:first').attr('src'));
        $('.big_pic img').attr('jqimg', $(this).attr('bigimg'));
    });

    //点击后移动的距离
    var left_num = -61;

    //整个ul超出显示区域的尺寸
    var li_length = ($('.ware_box li').width() + 6) * $('.ware_box li').length - 305;

    $('.right_btn').click(function () {
        var posleft_num = $('.ware_box ul').position().left;
        if ($('.ware_box ul').position().left > -li_length) {
            $('.ware_box ul').css({'left': posleft_num + left_num});
        }
    });

    $('.left_btn').click(function () {
        var posleft_num = $('.ware_box ul').position().left;
        if ($('.ware_box ul').position().left < 0) {
            $('.ware_box ul').css({'left': posleft_num - left_num});
        }
    });

    // 加入购物车弹出层
    $('.close_btn').click(function () {
        $('.ware_cen').slideUp('slow');
    });


    /* 促销商品倒计时 */
    var theDaysBox = $('.J_Countdown_GoodsPromotion').find('.J_NumDays');
    var theHoursBox = $('.J_Countdown_GoodsPromotion').find('.J_NumHours');
    var theMinsBox = $('.J_Countdown_GoodsPromotion').find('.J_NumMins');
    var theSecsBox = $('.J_Countdown_GoodsPromotion').find('.J_NumSeconds');

    countdown(theDaysBox, theHoursBox, theMinsBox, theSecsBox);

});

